package com.msmall.community.team.dao.hiberante;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.team.dao.TeamUserDao;
import com.msmall.community.team.model.TeamUser;

public class TeamUserDaoHiberante extends BaseDaoHibernate<TeamUser,Long> implements TeamUserDao{
	
	public TeamUserDaoHiberante(){
		super(TeamUser.class);
	}

	public TeamUser load(Long userid, Long teamid) throws Exception {
		try{
			String queryString = "from TeamUser where userid=? and teamid=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{userid, teamid});
			if(list != null && list.size() ==1)
				return (TeamUser) list.get(0);
			return null;
		}catch(Exception e){
			throw e;
		}
	}

	public List<TeamUser> loadByTeam(Long teamid) throws Exception{
		try{
			String queryString = "from TeamUser where teamid=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{teamid});
			return list;
		}catch(Exception e){
			throw e;
		}
	}

	public List<TeamUser> loadByUser(Long userid) throws Exception {
		try{
			String queryString = "from TeamUser where userid=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{userid});
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 获得组的创建者 
	 */
	public TeamUser getTeamCreator(Long teamid) throws Exception{
		try{
			String queryString = "from TeamUser where teamid=? and position=" + TeamUser.POSITION_CHAIRMAN;
			List list = getHibernateTemplate().find(queryString, new Object[]{teamid});
			return (TeamUser) list.get(0);
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 获得组的管理者,并且状态是TEAM.STATUS_NORMAL
	 *   creator: true 包括创建者 false:不包括创建者
	 */
	public List<TeamUser> getTeamManagers(Long teamid,boolean creator) throws Exception{
		try{
			if(creator){
				String queryString = "from TeamUser where teamid=? and (position=? or position=?) and status=?";
				List list = getHibernateTemplate().find(queryString, new Object[]{teamid, TeamUser.POSITION_VICE_CHAIRMAN, TeamUser.POSITION_CHAIRMAN,TeamUser.STATUS_NORMAL});
				return list;
			}else{
				String queryString = "from TeamUser where teamid=? and position=? and status=?";
				List list = getHibernateTemplate().find(queryString, new Object[]{teamid, TeamUser.POSITION_VICE_CHAIRMAN, TeamUser.STATUS_NORMAL});
				return list;
			}
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 获得组的普通成员,并且状态是TEAM.STATUS_NORMAL，不包括创建者和管理者 
	 */
	public List<TeamUser> getTeamMembers(Long teamid) throws Exception{
		try{
			String queryString = "from TeamUser where teamid=? and position=? and status=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{teamid, TeamUser.POSITION_NORMAL, TeamUser.STATUS_NORMAL});
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 根据组员状态获得组用户信息
	 * type TeamUser.STATUS_xx,如果为-1则获得所有用户
	 * @author zhzy
	 */
	public List<TeamUser> getMembers(Long teamid, int type) throws Exception{
		try{
			if(type > 0){
				String queryString = "from TeamUser where teamid=? and status=?";
				List list = getHibernateTemplate().find(queryString, new Object[]{teamid, type});
				return list;
			}else{
				String queryString = "from TeamUser where teamid=?";
				List list = getHibernateTemplate().find(queryString, new Object[]{teamid});
				return list;
			}
		}catch(Exception e){
			throw e;
		}
	}
}
